package com.gitee.zycra.future.job.web.config;

import com.gitee.zycra.future.job.web.handler.JobWebSocketHandler;
import com.gitee.zycra.future.job.web.interceptor.NodeInfoInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * @author zycra
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Value("${allowedOrigins}")
    private String allowedOrigins;

    @Autowired
    private JobWebSocketHandler jobWebSocketHandler;

    @Autowired
    private NodeInfoInterceptor nodeInfoInterceptor;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(jobWebSocketHandler, "syncJobInfo")
                .addInterceptors(nodeInfoInterceptor)
                .setAllowedOrigins(allowedOrigins);
    }
}
